package leecode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class LeetCode33 {
    Stack<Integer>stack=new Stack<>();
    Boolean b=true;
    public boolean isValidBST(TreeNode root) {
        istree(root);
        return b;
    }

    private void istree(TreeNode root) {
        if(root==null){
            return ;
        }
        istree(root.left);
        if(stack.empty()){
            stack.add(root.val);
        }else {
            if(stack.peek()>root.val){
                b=false;
            }else {
                stack.add(root.val);
            }
        }
        istree(root.right);
        return ;
    }
    //牛逼方法
    long last=Long.MAX_VALUE;
    public boolean isValidBST2(TreeNode root) {
        if(root==null){
            return true;
        }
        if(isValidBST(root.left)){
            if(last<root.val){
                last=root.val;
                return isValidBST(root.right);
            }
        }
        return false;
    }
}
